/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.h2t.loveplace.views;

import com.h2t.loveplace.main.LoveplaceMidlet;
import com.h2t.loveplace.models.Place;
import com.h2t.loveplace.utils.Storage;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BoxLayout;
import java.io.IOException;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

/**
 *
 * @author TungHD
 */
public class ViewDetailPlace extends Form implements ActionListener{
    private LoveplaceMidlet main;
    //private Form fmDetailPlace;
    private Label lbPicture;
    private Label lbTitle;
    private TextArea taDetail;
    private Container container;
    private Command cmBack;
    private Command cmdMap;
    
    private Place place;
    
    public ViewDetailPlace(LoveplaceMidlet main){
        this.main = main;
        //fmDetailPlace = new Form("Detail Place");
        //fmDetailPlace.
                setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        cmBack = new Command("Back");
        cmdMap = new Command("Map");
        //fmDetailPlace.
        setBackCommand(cmBack); 
        addCommand(cmBack);
                addCommand(cmdMap);
                setDefaultCommand(cmdMap);
        //fmDetailPlace.
                addCommandListener(this);
        // soure image can thay doi
      //  Image image = null;
        try {
          //  image = Image.createImage("/com/h2t/loveplace/views/images/place1.jpg");
        //   image = image.scaled(225, 225);
            //fmDetailPlace.
            setBgImage(Image.createImage("/photo/new-bg.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
        // hinh anh
        lbPicture = new Label();
        lbPicture.setSize(new Dimension(200, 200));
        lbPicture.setHeight(200);
        lbPicture.setWidth(200);
        // tieu de can thay doi
        lbTitle = new Label("");
        lbTitle.getStyle().setBgTransparency(1);
        lbTitle.getStyle().setFgColor(1);
        lbTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
        // mo ta
        taDetail = new TextArea("", 7, 8);
        taDetail.setEnabled(false);
        
        container = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        container.addComponent(lbPicture);
        container.addComponent(lbTitle);
        container.addComponent(taDetail);
        //fmDetailPlace.
                addComponent(container);
       // fmDetailPlace.show();
    }
    
    public void setPlace(Place place) {
        this.place = place;
        lbTitle.setText(place.getTitle());
        taDetail.setText(place.getDescription());
        if(place.getImg() != null) {
            lbPicture.setIcon(Storage.loadImg(place.getImg()));
        } else {
            try {
                lbPicture.setIcon(Image.createImage("/photo/place1.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        repaint();
    }
    
    public void actionPerformed(ActionEvent ae) {
        Object obj = ae.getSource();
        if (obj == cmBack) {
            main.showMainView();
        } else if(ae.getCommand() == cmdMap) {
            main.getMap().setPlace(place);
            main.showMapView();
        }
    }

    
}
